package com.ugoodtech.mdcc.core.service.impl;

import cn.hutool.core.date.DateUtil;
import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_Complete;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_NeedFollowUp;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_Plan;
import com.ugoodtech.mdcc.core.dto.JsonResponse;
import com.ugoodtech.mdcc.core.helper.SearchVoId;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.CoreWeekServiceReportService;
import com.ugoodtech.mdcc.core.service.SearchPermissionsService;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CoreWeekServiceReportServiceImpl implements CoreWeekServiceReportService {

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;

    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Autowired
    private StationReportRepository stationReportRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private SearchPermissionsService searchPermissionsService;

    @Autowired
    private CustomerRegularReportRepository customerRegularReportRepository;


    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);

    @Override
    public JsonResponse statWeekReport(User clientUser, String province, String brand, String category,
                                       String stationName, Long serviceStartTime, Long serviceEndTime,
                                       Long startTime, Long endTime, String serviceType, String department,
                                       Integer serviceNum, String userName, String city,String customerStat,
                                       String customerType,String paintBrand,String productPosition,String reportStatus,
                                       String serviceReportStatus,Boolean type,String score,Boolean paintType,
                                       String payService,String sentTime,String serviceTime,String stationNum) {
        UserProfile profile = userProfileRepository.findByUserId(clientUser.getId());
        SearchVoId searchVoId;
        if (clientUser.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(profile, province, brand);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(profile, province, brand);
        }
        if (searchVoId.getResultStationId().isEmpty()) {
            return JsonResponse.successResponseWithData(new VstarUserKpiReport());
        }

        String[] cities = null;
        String[] categories = null;
        Long[] customerStats = null;
        Long[] customerTypes = null;
        String[] paintBrands = null;
        String[] productPositions = null;
        String[] serviceTypes = null;
        String[] serviceReportStatuses = null;
        String[] reportStatuses = null;
        String[] departments = null;

        if (StringUtils.isNotEmpty(city)){
            cities = city.split(",");
        }
        if (StringUtils.isNotEmpty(category)){
            categories = category.split(",");
        }
        if (StringUtils.isNotEmpty(paintBrand)){
            paintBrands = paintBrand.split(",");
        }
        if (StringUtils.isNotEmpty(productPosition)){
            productPositions = productPosition.split(",");
        }
        if (StringUtils.isNotEmpty(serviceType)){
            serviceTypes = serviceType.split(",");
        }
        if (StringUtils.isNotEmpty(department)){
            departments = department.split(",");
        }
        if (StringUtils.isNotEmpty(serviceReportStatus)){
            serviceReportStatuses = serviceReportStatus.split(",");
        }
        if (StringUtils.isNotEmpty(reportStatus)){
            reportStatuses = reportStatus.split(",");
        }
        if (StringUtils.isNotEmpty(customerStat)){
            customerStats = (Long[]) ConvertUtils.convert(customerStat.split(","),Long.class);
        }
        if (StringUtils.isNotEmpty(customerType)){
            customerTypes = (Long[]) ConvertUtils.convert(customerType.split(","),Long.class);
        }

        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.category.eq(Constants.SERVICE_REPORT));
        builder.and(qStationReport.station.id.in(searchVoId.getResultStationId()));
        if (serviceStartTime != null && serviceStartTime != 0){
            builder.and(qStationReport.startServiceTime.goe(new Date(serviceStartTime)));
        }if(serviceEndTime != null && serviceEndTime != 0) {
            builder.and(qStationReport.endServiceTime.lt(new Date(serviceEndTime)));
        }
        if (startTime != null && startTime != 0){
            builder.and(qStationReport.updateTime.after(new Date(startTime)));
        }if(endTime != null && endTime != 0) {
            builder.and(qStationReport.updateTime.before(new Date(endTime)));
        }
        if (categories!=null) {
            builder.and(qStationReport.station.category.in(categories));
        }
        if (cities!=null) {
            builder.and(qStationReport.station.city.in(cities));
        }
        if (reportStatuses!=null){
            builder.and(qStationReport.exceptionType.in(reportStatuses));
        }
        if (customerStats!=null) {
            builder.and(qStationReport.station.customerStat.in(customerStats));
        }
        if (customerTypes!=null) {
            builder.and(qStationReport.station.customerType.in(customerTypes));
        }
        if (paintBrands!=null) {
            BooleanBuilder paintBrandBuilder = new BooleanBuilder();
            for (String pBrand:paintBrands){
                paintBrandBuilder.or(qStationReport.station.paintBrand.like("%" + pBrand + "%"));
            }
            builder.and(paintBrandBuilder);
        }
        if (productPositions!=null) {
            BooleanBuilder ppBuilder = new BooleanBuilder();
            for (String pp:productPositions){
                ppBuilder.or(qStationReport.station.productPosition.like("%" + pp + "%"));
            }
            builder.and(ppBuilder);
        }
        if (cities!=null) {
            builder.and(qStationReport.station.city.in(cities));
        }
        if (categories!=null) {
            builder.and(qStationReport.station.category.in(categories));
        }
        if (StringUtils.isNotEmpty(stationName)) {
            builder.and(qStationReport.station.name.like("%" + stationName + "%"));
        }

        if (paintType != null){
            builder.and(qStationReport.station.isPaint.eq(paintType?1:0));
        }
        if (serviceTypes != null || serviceReportStatuses != null ) {
            //先从报告答案中找出所有填写过ids中任何一个id的答案
            QStationRegularServiceReport qStationRegularServiceReport = QStationRegularServiceReport.stationRegularServiceReport;
            BooleanBuilder regularBuilder = new BooleanBuilder();
            regularBuilder.and(qStationRegularServiceReport.deleted.eq(false));
            regularBuilder.and(qStationRegularServiceReport.enabled.eq(true));
            BooleanBuilder builderOr = new BooleanBuilder();
            if (serviceTypes != null) {
                for (String id : serviceTypes) {
                    builderOr.or(qStationRegularServiceReport.answerId.like("%" + id + "%"));
                }
                regularBuilder.and(builderOr);
            }
            if (serviceReportStatuses != null){
                for (String id : serviceReportStatuses) {
                    builderOr.or(qStationRegularServiceReport.answerId.like("%" + id + "%"));
                }
                regularBuilder.and(builderOr);
            }
            List<StationRegularServiceReport> list = (List<StationRegularServiceReport>) stationRegularServiceReportRepository.findAll(regularBuilder);
            if (list == null || list.size()==0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList()));
            }
            List<Long> Ids = new ArrayList<>();
            for (StationRegularServiceReport report : list) {
                if (report.getReportId() != null) {
                    Ids.add(Long.parseLong(report.getReportId()));
                }
            }
            builder.and(qStationReport.station.id.in(Ids));
        }
        if (StringUtils.isNotEmpty(userName) || departments != null) {
            QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            booleanBuilder.and(qUserProfile.user.userFlag.in(0,2));
            boolean flag = false;
            if (StringUtils.isNotEmpty(userName)) {
                flag = true;
                booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            }
            if (departments != null){
                flag = true;
                booleanBuilder.and(qUserProfile.user.userFlag.in(0,2));
                booleanBuilder.and(qUserProfile.user.userType.in(UserType.admin,UserType.dealers,UserType.insiders));
                for (String str:departments){
                    booleanBuilder.or(qUserProfile.departmentList.any().name.eq(str));
                }
            }
            if (flag) {
                List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
                if (userProfileList == null || userProfileList.size() == 0) {
                    return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList()));
                }
                List<Long> userIds = new ArrayList<>();
                for (UserProfile user : userProfileList) {
                    userIds.add(user.getUser().getId());
                }
                BooleanBuilder colorBuilder = new BooleanBuilder();
                colorBuilder.and(qStationColorReport.deleted.eq(false));
                colorBuilder.and(qStationColorReport.enabled.eq(true));
                colorBuilder.and(qStationColorReport.stationReport.station.report_finished.in(1, 2, 3));
                colorBuilder.and(qStationColorReport.stationReport.user.id.in(userIds));
                List<StationColorReport> list = (List<StationColorReport>) stationColorReportRepository.findAll(colorBuilder);
                if (list == null || list.size() == 0) {
                    return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList()));
                }
                List<Long> ids = new ArrayList<>();
                for (StationColorReport report : list) {
                    if (report.getReportId() != null) {
                        ids.add(Long.parseLong(report.getReportId()));
                    }
                }
                builder.and(qStationReport.id.in(ids));
            }
        }
        if (StringUtils.isNotEmpty(stationNum)) {
            builder.and(qStationReport.station.stationNum.like("%" + stationNum + "%"));
        }

        if (payService != null || sentTime!=null || serviceTime!=null){
            QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
            BooleanBuilder builderStationColor = new BooleanBuilder();
            builderStationColor.and(qStationColorReport.deleted.eq(false));
            builderStationColor.and(qStationColorReport.enabled.eq(true));
            boolean flag = false;
            if (payService!=null){
                flag = true;
                builderStationColor.and(qStationColorReport.answerId.like("%"+payService+"%"));
            }
            if (sentTime!=null){
                flag = true;
                builderStationColor.and(qStationColorReport.answerId.like("%"+sentTime+"%"));
            }
            if (serviceTime!=null){
                flag = true;
                builderStationColor.and(qStationColorReport.answerId.like("%"+serviceTime+"%"));
            }
            if (flag) {
                List<StationColorReport> stationColorReports = (List<StationColorReport>) stationColorReportRepository.findAll(builderStationColor);
                if (stationColorReports == null || stationColorReports.size() == 0) {
                    return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList()));
                }
                List<Long> Ids = new ArrayList<>();
                for (StationColorReport report : stationColorReports) {
                    Ids.add(report.getStationReport().getStation().getId());
                }
                builder.and(qStationReport.station.id.in(Ids));
            }
        }
        if (type != null) {
            QCustomerRegularReport qCustomerRegularReport = QCustomerRegularReport.customerRegularReport;
            BooleanBuilder builderCustomer = new BooleanBuilder();
            builderCustomer.and(qCustomerRegularReport.deleted.eq(false));
            builderCustomer.and(qCustomerRegularReport.enabled.eq(true));
            builderCustomer.and(qCustomerRegularReport.type.eq(type));
            List<CustomerRegularReport> customerRegularReports = (List<CustomerRegularReport>) customerRegularReportRepository.findAll(builderCustomer);
            if (customerRegularReports == null || customerRegularReports.size()==0){
                return JsonResponse.successResponseWithPageData(new PageImpl(new ArrayList()));
            }
            Long[] ids = new Long[customerRegularReports.size()];
            for (int i = 0; i < customerRegularReports.size(); i++) {
                ids[i] = customerRegularReports.get(i).getRegularReport().getId();
            }
            builder.and(qStationReport.id.in(ids));
        }
        Sort sort = new Sort(Sort.Direction.DESC, "user.id", "updateTime");
        List<StationReport> stationReportList = (List<StationReport>) stationReportRepository.findAll(builder, sort);
        List<Long> serviceReportId = new ArrayList<>();
        for (StationReport stationReport : stationReportList) {
            serviceReportId.add(stationReport.getId());
        }
        if (!serviceReportId.isEmpty()) {
            if (StringUtils.isNotEmpty(score)) {
                QCustomerRegularReport qCustomerRegularReport = QCustomerRegularReport.customerRegularReport;
                BooleanBuilder booleanBuilder = new BooleanBuilder();
                booleanBuilder.and(qCustomerRegularReport.regularReport.id.in(serviceReportId));
                String[] str = new String[61];
                for (int i = 0; i < str.length; i++) {
                    str[i] = i + "";
                }
                String[] notStr = new String[39];
                int index = 61;
                for (int i = 0; i < 39; i++) {
                    notStr[i] = index + "";
                    index += 1;
                }
                if (StringUtils.isNotEmpty(score)) {
                    if (score.equals(Constants.FULL_MARKS)) {
                        booleanBuilder.and(qCustomerRegularReport.score.eq("100"));
                    }
                    if (score.equals(Constants.MORE_THAN_SIXTY)) {
                        booleanBuilder.and(qCustomerRegularReport.score.in(notStr));
                    }
                    if (score.equals(Constants.BELOW_THAN_SIXTY)) {
                        booleanBuilder.and(qCustomerRegularReport.score.in(str));
                    }
                }
                List<CustomerRegularReport> customerRegularReportList = (List<CustomerRegularReport>) customerRegularReportRepository.findAll(booleanBuilder);
                List<StationReport> reportList = new ArrayList<>();
                for (CustomerRegularReport customerRegularReport : customerRegularReportList) {
                    reportList.add(customerRegularReport.getRegularReport());
                }
                stationReportList = reportList;
            }
        }
        VstarUserKpiReport vstarUserKpiReport = new VstarUserKpiReport();
        List<VstarUserKpiReport_Complete> vstarUserKpiReport_complete = new ArrayList<>();
        List<VstarUserKpiReport_NeedFollowUp> vstarUserKpiReport_needFollowUp = new ArrayList<>();
        List<VstarUserKpiReport_Plan> vstarUserKpiReport_plan = new ArrayList<>();
        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);
            if (answer != null) {
                //获取其他信息
                VstarUserKpiReport_Complete complete = new VstarUserKpiReport_Complete();
                complete.setReportId(stationReport.getId());
                UserProfile workUser = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (workUser != null && StringUtils.isNotEmpty(workUser.getRealName())) {
                    complete.setWorkUser(workUser.getRealName());
                } else {
                    complete.setWorkUser("N/A");
                }
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            complete.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        complete.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            complete.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        complete.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    complete.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    complete.setProduct_position(stringBuffer.toString());
                }
                complete.setBrand(stationReport.getStation().getBrand());
                complete.setProvince(stationReport.getStation().getProvince());
                complete.setTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                try {
                    String[] targeIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targeIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("191")) {
                                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                    stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                                }
                            } else if (id.equals("189")) {
                                if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                    stringBuffer.append(stationReport.getAnswerOther() + ",");
                                } else {
                                    stringBuffer.append("未填写内容,请联系此工作人员");
                                }
                            } else {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                    stringBuffer.append(answerOption.getName() + ",");
                                }
                            }

                        }
                    }
                    complete.setWorkType(stringBuffer.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                    complete.setWorkType("N/A");
                }
                complete.setCustomerName(stationReport.getStation().getName());
                complete.setCategory(stationReport.getStation().getCategory());
                complete.setStationNum(stationReport.getStation().getStationNum());
                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    complete.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    complete.setServiceCompany("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    Station station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        complete.setAddress(station.getAddress());
                    } else {
                        String province1 = "";
                        String city1 = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province1 = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city1 = station.getCity();
                        }
                        complete.setAddress(province1 + city1);
                    }
                } else {
                    complete.setAddress("");
                }

                //获取服务自我评价
                regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 157L);
                complete.setServicePoint((regularServiceReport!=null&&regularServiceReport.getAnswerOther()!=null)?regularServiceReport.getAnswerOther():"N/A");
                vstarUserKpiReport_complete.add(complete);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_complete(vstarUserKpiReport_complete);

        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);
            if ((answer != null && !answer.getAnswerId().equals("181")) && !answer.getAnswerId().equals("354") || answer == null) {
                VstarUserKpiReport_NeedFollowUp needFollowUp = new VstarUserKpiReport_NeedFollowUp();
                needFollowUp.setReportId(stationReport.getId());
                UserProfile workUser = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (workUser != null && StringUtils.isNotEmpty(workUser.getRealName())) {
                    needFollowUp.setWorkUser(workUser.getRealName());
                } else {
                    needFollowUp.setWorkUser("N/A");
                }
                needFollowUp.setProvince(stationReport.getStation().getProvince());
                needFollowUp.setCategory(stationReport.getStation().getCategory());
                needFollowUp.setStationNum(stationReport.getStation().getStationNum());
                needFollowUp.setBrand(stationReport.getStation().getBrand());

                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            needFollowUp.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        needFollowUp.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            needFollowUp.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        needFollowUp.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    needFollowUp.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    needFollowUp.setProduct_position(stringBuffer.toString());
                }

                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 156L);
                if (regularServiceReport != null) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(regularServiceReport.getAnswerId()));
                    List<String> departmentList = new ArrayList<>();
                    departmentList.add(answerOption.getName());
                    if (answerOption.getId().equals(182)) {
                        StationReport deparmentReport = regularServiceReport.getStationReport();
                        String[] departmentIds = deparmentReport.getDeparmentIds().split(",");
                        for (String id : departmentIds) {
                            if (StringUtils.isEmpty(id)) {
                                continue;
                            }
                            Department department1 = departmentRepository.findOne(Long.valueOf(id));
                            departmentList.add(department1.getTotalName());
                        }
                    }
                    needFollowUp.setAbnormalSituation(departmentList);
                } else {
                    List<String> stringList = new ArrayList<>();
                    stringList.add("");
                    needFollowUp.setAbnormalSituation(stringList);
                }

                regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                needFollowUp.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                if (regularServiceReport == null) {
                    needFollowUp.setCuntomerName("N/A");
                    needFollowUp.setWorkType("N/A");
                } else {
                    String[] targerIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targerIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("191")) {
                                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                    stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                                }
                            } else if (id.equals("189")) {
                                if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                    stringBuffer.append(stationReport.getAnswerOther() + ",");
                                } else {
                                    stringBuffer.append("未填写内容,请联系此工作人员");
                                }
                            } else {
                                QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswerOption!=null&&questionAnswerOption.getName()!=null) {
                                    String targer = questionAnswerOption.getName();
                                    stringBuffer.append(targer + ",");
                                }
                            }

                        }
                    }
                    needFollowUp.setCuntomerName(stationReport.getStation().getName());
                    needFollowUp.setWorkType(stringBuffer.toString());
                }
                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    needFollowUp.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    needFollowUp.setServiceCompany("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    Station station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        needFollowUp.setAddress(station.getAddress());
                    } else {
                        String province1 = "";
                        String city1 = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province1 = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city1 = station.getCity();
                        }
                        needFollowUp.setAddress(province1 + city1);
                    }
                } else {
                    needFollowUp.setAddress("");
                }


                //获取服务自我评价
                try {
                    regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 157L);
                    needFollowUp.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    needFollowUp.setServicePoint("N/A");
                }
                vstarUserKpiReport_needFollowUp.add(needFollowUp);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_needFollowUp(vstarUserKpiReport_needFollowUp);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
        Station station = null;
        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 354L);
            if (answer != null) {
                VstarUserKpiReport_Plan plan = new VstarUserKpiReport_Plan();
                plan.setReportId(stationReport.getId());
                plan.setProvice(stationReport.getStation().getProvince());
                plan.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                plan.setCategory(stationReport.getStation().getCategory());
                plan.setStationNum(stationReport.getStation().getStationNum());
                plan.setBrand(stationReport.getStation().getBrand());
                plan.setStartTime(stationReport.getServiceStartTime());
                plan.setEndTime(stationReport.getServiceEndTime());
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            plan.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        plan.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            plan.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        plan.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    plan.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    plan.setProduct_position(stringBuffer.toString());
                }
                //封装工作人员
                if (stationReport.getUser() != null) {
                    UserProfile userProfile = userProfileRepository.findByUserId(stationReport.getUser().getId());
                    if (userProfile != null && StringUtils.isNotEmpty(userProfile.getRealName())) {
                        plan.setWorkUser(userProfile.getRealName());
                    } else {
                        plan.setWorkUser("N/A");
                    }
                } else {
                    plan.setWorkUser("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        plan.setAddress(station.getAddress());
                    } else {
                        String province1 = "";
                        String city1 = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province1 = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city1 = station.getCity();
                        }
                        plan.setAddress(province1 + city1);
                    }
                } else {
                    plan.setAddress("");
                }

                //封装客户名称
                if (stationReport.getStation() != null) {
                    station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getName())) {
                        plan.setCustomerUser(station.getName());
                    } else {
                        plan.setCustomerUser("N/A");
                    }
                } else {
                    plan.setCustomerUser("N/A");
                }

                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    plan.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    plan.setServiceCompany("N/A");
                }

                //获取服务自我评价
                try {
                    StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 157L);
                    plan.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    plan.setServicePoint("N/A");
                }
                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerId())) {
                    String[] targeIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targeIds) {
                        if (id.equals("191")) {
                            if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                            }
                        } else if (id.equals("189")) {
                            if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                stringBuffer.append(stationReport.getAnswerOther() + ",");
                            } else {
                                stringBuffer.append("未填写内容,请联系此工作人员");
                            }
                        } else {
                            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                stringBuffer.append(answerOption.getName() + ",");
                            }
                        }

                    }
                    plan.setWorkType(stringBuffer.toString());
                } else {
                    plan.setWorkType("");
                }
                vstarUserKpiReport_plan.add(plan);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_Plan(vstarUserKpiReport_plan);
        return JsonResponse.successResponseWithData(vstarUserKpiReport);
    }
    @Override
    public JsonResponse statWeekReport(User clientUser, String userName, String stationName, String provinces,
                                       String brandname, String category, Long serviceStartTime, Long serviceEndTime, Boolean isFollowUp, Boolean isCustomer,
                                       String score, Long serviceType, Long startTime, Long endTime, Long departmentId) {
        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.category.eq(Constants.SERVICE_REPORT));
        UserProfile profile = userProfileRepository.findByUserId(clientUser.getId());
        SearchVoId searchVoId;
        if (clientUser.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(profile, provinces, brandname);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(profile, provinces, brandname);
        }
        if (searchVoId.getResultStationId().isEmpty()) {
            return JsonResponse.successResponseWithData(new VstarUserKpiReport());
        }
        builder.and(qStationReport.station.id.in(searchVoId.getResultStationId()));
        if (null != isFollowUp) {
            if (isFollowUp) {
                builder.and(qStationReport.deparmentIds.isNotNull());
            } else {
                builder.and(qStationReport.deparmentIds.isNull());
            }
        }
        if (serviceStartTime != null && serviceStartTime != 0 && serviceEndTime != null && serviceEndTime != 0) {
            builder.and(qStationReport.startServiceTime.after(new Date(serviceStartTime)));
            builder.and(qStationReport.endServiceTime.before(new Date(serviceEndTime)));
        }
        if (startTime != null && startTime != 0 && endTime != null && endTime != 0) {
            builder.and(qStationReport.updateTime.after(new Date(startTime)));
            builder.and(qStationReport.updateTime.before(new Date(endTime)));
        }
        if (StringUtils.isNotEmpty(category)) {
            builder.and(qStationReport.station.category.eq(category));
        }
        List<Long> userIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(userName) || null != departmentId) {
            List<Long> departmentIdList = new ArrayList<>();
            if (null != departmentId) {
                departmentIdList.add(departmentId);
                List<Department> departmentList = departmentRepository.getParentDeprtments();
                List<Long> departmentIds = new ArrayList<>();
                for (Department department : departmentList) {
                    departmentIds.add(department.getId());
                }
                if (departmentIds.contains(departmentId)) {
                    departmentList = departmentRepository.selectParentDeparmentL(departmentId);
                    for (Department department : departmentList) {
                        departmentIdList.add(department.getId());
                    }
                }
            }
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            if (!departmentIdList.isEmpty()) {
                booleanBuilder.and(qUserProfile.departmentList.any().id.in(departmentIdList));
            }
            List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
            for (UserProfile user : userProfileList) {
                userIds.add(user.getUser().getId());
            }
            builder.and(qStationReport.user.id.in(userIds));
        }

        if (StringUtils.isNotEmpty(stationName)) {
            builder.and(qStationReport.station.name.like("%" + stationName + "%"));
        }
        if (null != serviceType && serviceType != 0) {
            builder.and(qStationReport.serviceTypAnswerIds.like("%" + serviceType + "%"));
        }
        Sort sort = new Sort(Sort.Direction.DESC, "user.id", "updateTime");
        List<StationReport> stationReportList = (List<StationReport>) stationReportRepository.findAll(builder, sort);
        List<Long> serviceReportId = new ArrayList<>();
        for (StationReport stationReport : stationReportList) {
            serviceReportId.add(stationReport.getId());
        }
        if (!serviceReportId.isEmpty()) {
            if (isCustomer != null || StringUtils.isNotEmpty(score)) {
                QCustomerRegularReport qCustomerRegularReport = QCustomerRegularReport.customerRegularReport;
                BooleanBuilder booleanBuilder = new BooleanBuilder();
                booleanBuilder.and(qCustomerRegularReport.regularReport.id.in(serviceReportId));
                if (isCustomer != null) {
                    if (isCustomer != null) {
                        if (isCustomer) {
                            booleanBuilder.and(qCustomerRegularReport.customerReport.isNotNull());
                        } else {
                            booleanBuilder.and(qCustomerRegularReport.customerReport.isNull());
                        }
                    }
                }
                String[] str = new String[61];
                for (int i = 0; i < str.length; i++) {
                    str[i] = i + "";
                }
                String[] notStr = new String[39];
                int index = 61;
                for (int i = 0; i < 39; i++) {
                    notStr[i] = index + "";
                    index += 1;
                }
                if (StringUtils.isNotEmpty(score)) {
                    if (score.equals(Constants.FULL_MARKS)) {
                        booleanBuilder.and(qCustomerRegularReport.score.eq("100"));
                    }
                    if (score.equals(Constants.MORE_THAN_SIXTY)) {
                        booleanBuilder.and(qCustomerRegularReport.score.in(notStr));
                    }
                    if (score.equals(Constants.BELOW_THAN_SIXTY)) {
                        booleanBuilder.and(qCustomerRegularReport.score.in(str));
                    }
                }
                List<CustomerRegularReport> customerRegularReportList = (List<CustomerRegularReport>) customerRegularReportRepository.findAll(booleanBuilder);
                List<StationReport> reportList = new ArrayList<>();
                for (CustomerRegularReport customerRegularReport : customerRegularReportList) {
                    reportList.add(customerRegularReport.getRegularReport());
                }
                stationReportList = reportList;
            }
        }
        VstarUserKpiReport vstarUserKpiReport = new VstarUserKpiReport();
        List<VstarUserKpiReport_Complete> vstarUserKpiReport_complete = new ArrayList<>();
        List<VstarUserKpiReport_NeedFollowUp> vstarUserKpiReport_needFollowUp = new ArrayList<>();
        List<VstarUserKpiReport_Plan> vstarUserKpiReport_plan = new ArrayList<>();
        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);
            if (answer != null) {
                //获取其他信息
                VstarUserKpiReport_Complete complete = new VstarUserKpiReport_Complete();
                complete.setReportId(stationReport.getId());
                UserProfile workUser = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (workUser != null && StringUtils.isNotEmpty(workUser.getRealName())) {
                    complete.setWorkUser(workUser.getRealName());
                } else {
                    complete.setWorkUser("N/A");
                }
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            complete.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        complete.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            complete.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        complete.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    complete.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    complete.setProduct_position(stringBuffer.toString());
                }
                complete.setBrand(stationReport.getStation().getBrand());
                complete.setProvince(stationReport.getStation().getProvince());
                complete.setTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                try {
                    String[] targeIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targeIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("191")) {
                                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                    stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                                }
                            } else if (id.equals("189")) {
                                if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                    stringBuffer.append(stationReport.getAnswerOther() + ",");
                                } else {
                                    stringBuffer.append("未填写内容,请联系此工作人员");
                                }
                            } else {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                    stringBuffer.append(answerOption.getName() + ",");
                                }
                            }

                        }
                    }
                    complete.setWorkType(stringBuffer.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                    complete.setWorkType("N/A");
                }
                complete.setCustomerName(stationReport.getStation().getName());
                complete.setCategory(stationReport.getStation().getCategory());
                complete.setStationNum(stationReport.getStation().getStationNum());
                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    complete.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    complete.setServiceCompany("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    Station station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        complete.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        complete.setAddress(province + city);
                    }
                } else {
                    complete.setAddress("");
                }

                //获取服务自我评价
                regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 157L);
                complete.setServicePoint((regularServiceReport!=null&&regularServiceReport.getAnswerOther()!=null)?regularServiceReport.getAnswerOther():"N/A");
                vstarUserKpiReport_complete.add(complete);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_complete(vstarUserKpiReport_complete);

        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);
            if ((answer != null && !answer.getAnswerId().equals("181")) && !answer.getAnswerId().equals("354") || answer == null) {
                VstarUserKpiReport_NeedFollowUp needFollowUp = new VstarUserKpiReport_NeedFollowUp();
                needFollowUp.setReportId(stationReport.getId());
                UserProfile workUser = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (workUser != null && StringUtils.isNotEmpty(workUser.getRealName())) {
                    needFollowUp.setWorkUser(workUser.getRealName());
                } else {
                    needFollowUp.setWorkUser("N/A");
                }
                needFollowUp.setProvince(stationReport.getStation().getProvince());
                needFollowUp.setCategory(stationReport.getStation().getCategory());
                needFollowUp.setStationNum(stationReport.getStation().getStationNum());
                needFollowUp.setBrand(stationReport.getStation().getBrand());

                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            needFollowUp.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        needFollowUp.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            needFollowUp.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        needFollowUp.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    needFollowUp.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    needFollowUp.setProduct_position(stringBuffer.toString());
                }

                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 156L);
                if (regularServiceReport != null) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(regularServiceReport.getAnswerId()));
                    List<String> departmentList = new ArrayList<>();
                    departmentList.add(answerOption.getName());
                    if (answerOption.getId().equals(182)) {
                        StationReport deparmentReport = regularServiceReport.getStationReport();
                        String[] departmentIds = deparmentReport.getDeparmentIds().split(",");
                        for (String id : departmentIds) {
                            if (StringUtils.isEmpty(id)) {
                                continue;
                            }
                            Department department = departmentRepository.findOne(Long.valueOf(id));
                            departmentList.add(department.getTotalName());
                        }
                    }
                    needFollowUp.setAbnormalSituation(departmentList);
                } else {
                    List<String> stringList = new ArrayList<>();
                    stringList.add("");
                    needFollowUp.setAbnormalSituation(stringList);
                }

                regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                needFollowUp.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                if (regularServiceReport == null) {
                    needFollowUp.setCuntomerName("N/A");
                    needFollowUp.setWorkType("N/A");
                } else {
                    String[] targerIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targerIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("191")) {
                                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                    stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                                }
                            } else if (id.equals("189")) {
                                if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                    stringBuffer.append(stationReport.getAnswerOther() + ",");
                                } else {
                                    stringBuffer.append("未填写内容,请联系此工作人员");
                                }
                            } else {
                                QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswerOption!=null&&questionAnswerOption.getName()!=null) {
                                    String targer = questionAnswerOption.getName();
                                    stringBuffer.append(targer + ",");
                                }
                            }

                        }
                    }
                    needFollowUp.setCuntomerName(stationReport.getStation().getName());
                    needFollowUp.setWorkType(stringBuffer.toString());
                }
                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    needFollowUp.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    needFollowUp.setServiceCompany("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    Station station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        needFollowUp.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        needFollowUp.setAddress(province + city);
                    }
                } else {
                    needFollowUp.setAddress("");
                }


                //获取服务自我评价
                try {
                    regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 157L);
                    needFollowUp.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    needFollowUp.setServicePoint("N/A");
                }
                vstarUserKpiReport_needFollowUp.add(needFollowUp);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_needFollowUp(vstarUserKpiReport_needFollowUp);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
        Station station = null;
        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 354L);
            if (answer != null) {
                VstarUserKpiReport_Plan plan = new VstarUserKpiReport_Plan();
                plan.setReportId(stationReport.getId());
                plan.setProvice(stationReport.getStation().getProvince());
                plan.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                plan.setCategory(stationReport.getStation().getCategory());
                plan.setStationNum(stationReport.getStation().getStationNum());
                plan.setBrand(stationReport.getStation().getBrand());
                plan.setStartTime(stationReport.getServiceStartTime());
                plan.setEndTime(stationReport.getServiceEndTime());
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            plan.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        plan.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            plan.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        plan.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    plan.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    plan.setProduct_position(stringBuffer.toString());
                }
                //封装工作人员
                if (stationReport.getUser() != null) {
                    UserProfile userProfile = userProfileRepository.findByUserId(stationReport.getUser().getId());
                    if (userProfile != null && StringUtils.isNotEmpty(userProfile.getRealName())) {
                        plan.setWorkUser(userProfile.getRealName());
                    } else {
                        plan.setWorkUser("N/A");
                    }
                } else {
                    plan.setWorkUser("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        plan.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        plan.setAddress(province + city);
                    }
                } else {
                    plan.setAddress("");
                }

                //封装客户名称
                if (stationReport.getStation() != null) {
                    station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getName())) {
                        plan.setCustomerUser(station.getName());
                    } else {
                        plan.setCustomerUser("N/A");
                    }
                } else {
                    plan.setCustomerUser("N/A");
                }

                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    plan.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    plan.setServiceCompany("N/A");
                }

                //获取服务自我评价
                try {
                    StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 157L);
                    plan.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    plan.setServicePoint("N/A");
                }
                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerId())) {
                    String[] targeIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targeIds) {
                        if (id.equals("191")) {
                            if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                            }
                        } else if (id.equals("189")) {
                            if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                stringBuffer.append(stationReport.getAnswerOther() + ",");
                            } else {
                                stringBuffer.append("未填写内容,请联系此工作人员");
                            }
                        } else {
                            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                stringBuffer.append(answerOption.getName() + ",");
                            }
                        }

                    }
                    plan.setWorkType(stringBuffer.toString());
                } else {
                    plan.setWorkType("");
                }
                vstarUserKpiReport_plan.add(plan);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_Plan(vstarUserKpiReport_plan);
        return JsonResponse.successResponseWithData(vstarUserKpiReport);
    }

}
